#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <mutex>
#include <unordered_map>
#include <boost/algorithm/string.hpp>
#include "cppjieba/Jieba.hpp"
#include "log.hpp"

namespace ns_util
{
    class FileUtil
    {
    public:
        static bool ReadFile(const std::string& file_path, std::string* out)
        {
            // 创建了一个std::ifstream类对象in
            // 并以指定的文件路径file_path和输入流模式std::ios::in来初始化该对象
            std::ifstream in(file_path, std::ios::in);
            if (!in.is_open())
            {
                std::cerr << "open file " << file_path << " error" << std::endl;
                return false;
            }

            // 如何理解getline读取到文件结束呢？
            // getline的返回值是一个引用，while循环判断的是bool
            // 本质是因为重载了强制类型转化
            
            std::string line;
            while (std::getline(in, line))
                *out += line;

            in.close();
            return true;
        }
    };

    class StringUtil
    {
    public:
        static void Split(const std::string& target, std::vector<std::string>* out, const std::string& sep)
        {
            // boost split
            boost::split(*out, target, boost::is_any_of(sep), boost::token_compress_on);
        }
    };

    const char* const DICT_PATH = "./dict/jieba.dict.utf8";
    const char* const HMM_PATH = "./dict/hmm_model.utf8";
    const char* const USER_DICT_PATH = "./dict/user.dict.utf8";
    const char* const IDF_PATH = "./dict/idf.utf8";
    const char* const STOP_WORD_PATH = "./dict/stop_words.utf8";

    class JiebaUtil
    {
    private:
        // 没有去掉暂停词写法
        // static cppjieba::Jieba jieba;

        // 去掉暂停词写法
        cppjieba::Jieba jieba;
        std::unordered_map<std::string, bool> stop_words;

        JiebaUtil()
            : jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH)
            {}
        JiebaUtil(const JiebaUtil&) = delete;

        static JiebaUtil* instance;
    public:
        static JiebaUtil* get_instance()
        {
            static std::mutex mtx;
            if (instance == nullptr)
            {
                mtx.lock();
                if (instance == nullptr)
                {
                    instance = new JiebaUtil();
                    instance->InitJiebaUtil();
                }
                mtx.unlock();
            }

            return instance;
        }

        void InitJiebaUtil()
        {
            std::ifstream in(STOP_WORD_PATH);
            if (!in.is_open())
            {
                LOG(FATAL, "load stop file error");
                return;
            }

            std::string line;
            while (std::getline(in, line))
            {
                std::pair<std::string, bool> word_pair(line, true);
                stop_words.insert(std::move(word_pair));

                // stop_words.insert({ line, true });
            }
            in.close();
        }

        void CutStringHelper(const std::string& src, std::vector<std::string>* out)
        {
            jieba.CutForSearch(src, *out);
            for (std::vector<std::string>::iterator iter = out->begin(); iter != out->end(); )
            // for (auto iter = out->begin(); iter != out->end(); )
            {
                std::unordered_map<std::string, bool>::iterator it = stop_words.find(*iter);
                // auto it = stop_words.find(*iter);
                if (it != stop_words.end())
                    // 说明当前string是暂停词，需要去掉
                    iter = out->erase(iter);
                else
                    iter++;
            }
        }
    public:
        static void CutString(const std::string& src, std::vector<std::string>* out)
        {
            // 没有去暂停词写法
            // jieba.CutForSearch(src, *out);

            // 去暂掉停词写法
            ns_util::JiebaUtil::get_instance()->CutStringHelper(src, out);
        }
    };
    
    // 没有去掉暂停词写法
    // cppjieba::Jieba JiebaUtil::jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);
    
    // 去暂掉停词写法
    JiebaUtil* JiebaUtil::instance = nullptr;
}